//
//  HCNewfeatureViewController.swift
//  Hweibome
//
//  Created by 何川 on 15/12/8.
//  Copyright © 2015年 hechuan. All rights reserved.
//

import UIKit

class HCNewfeatureViewController: UIViewController,UIScrollViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
//懒加载控件
    private lazy var  scrollView:UIScrollView =
    {
        let scrollView = UIScrollView()
        scrollView.delegate = self
        scrollView.pagingEnabled = true
        scrollView.bounces = false
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()
    // 分页控件
    private lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        // 默认情况下,子控件如果超出父控件的范围,是可以看得到.
        // 指定 clipsToBounds 代表把超出部分干掉
        // pageControl.clipsToBounds = true
        // 指定选中颜色
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        // 指定默认颜色
        pageControl.pageIndicatorTintColor = UIColor.purpleColor()
        return pageControl
    }()
    private lazy var enterButton:UIButton =
    {
        let button = UIButton()
        button.addTarget(self, action: "enterButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        button.setTitle("进入微博", forState: .Normal)
        button.sizeToFit()
        return button
    }()
    
    private lazy var sharedButton : UIButton =
    {
        let button = UIButton()
        button.addTarget(self, action: "sharedButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "new_feature_share_true"), forState: .Selected)
        button.setImage(UIImage(named: "new_feature_share_false"), forState: .Normal)
        button.setTitle("分享到微博", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.sizeToFit()
        return button
    }()
    private func setupUI()
    {
     scrollView.frame = view.bounds
        view.addSubview(scrollView)
        pageControl.frame = CGRect(x: scrollView.frame.width*0.5, y: scrollView.frame.height - 100, width: 0, height: 0)
        view.addSubview(pageControl)
        let count = 4
        for i in 0..<count
        {
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i + 1)"))
            imageView.frame = CGRect(x: CGFloat(i)*scrollView.frame.width, y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
            scrollView.addSubview(imageView)
            if i == count-1{
                setupLastPage(imageView)
            }
        }
        scrollView.contentSize = CGSize(width: CGFloat(count)*scrollView.frame.width, height: 0)
        pageControl.numberOfPages = count
    }
    
    @objc private func sharedButtonClick(button:UIButton)
    {
        button.selected = !button.selected
    }
    @objc private func enterButtonClick()
    {
        printLog("enterButtonClick")
        NSNotificationCenter.defaultCenter().postNotificationName(HCSwitchRootVCNotifation, object: nil)
    }
    
    
    
    
    private func setupLastPage(lastPage:UIView)
    {
        lastPage.userInteractionEnabled = true
        enterButton.center = CGPoint (x: lastPage.frame.width*0.5, y: lastPage.frame.height - 150)
        sharedButton.center = CGPoint (x: enterButton.center.x, y: CGRectGetMinY(enterButton.frame)-30)
        
        lastPage.addSubview(enterButton)
        lastPage.addSubview(sharedButton)
    }
    
    
 //MARK: - scrollView delegate
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let page = scrollView.contentOffset.x/scrollView.frame.width + 0.5
        //printLog(page)
        pageControl.currentPage = Int(page)
    }
}
